package com.idopartx.phonelightning.utils

import android.content.Intent
import android.util.Log
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.idopartx.phonelightning.application.CLApplication
import com.idopartx.phonelightning.ui.FullVideoActivity
import com.idopartx.phonelightning.ui.SplashActivity
import com.ido.switchmodel.SMHolder

class ProcessLifecycleObserver(val application: CLApplication) : DefaultLifecycleObserver {
    //是否第一次进入
    private var isOnCreate = true

    //不显示闪屏的界面列表
    var limitName = arrayOf(
        "SplashActivity"
    )
    private var lastTime = 0L//退出时间
    var nowShowActivityName: String? = null//当前显示闪屏的名字
    private var limitTime = 1//间隔时间

    override fun onCreate(owner: LifecycleOwner) {
        Log.e("AppObserver", "onCreate")
    }

    override fun onStart(owner: LifecycleOwner) {
        if (isOnCreate) {
            isOnCreate = false
        } else {
            check()
        }
        Log.e("AppObserver", "onStart")
    }

    override fun onResume(owner: LifecycleOwner) {
        Log.e("AppObserver", "onResume")
    }

    override fun onPause(owner: LifecycleOwner) {
        Log.e("AppObserver", "onPause")
    }

    override fun onStop(owner: LifecycleOwner) {
        Log.e("AppObserver", "onStop")
        lastTime = System.currentTimeMillis()
    }

    private fun check() {
        try {
            //计算时间限制
            val mode = SMHolder.getInstance().getHotSplash().getHotMode(this.application)
            limitTime = SMHolder.getInstance().getHotSplash().getHotIntervalTime(this.application)
            val time = ((System.currentTimeMillis() - lastTime) % (1000 * 60)) / 1000
            if (time <= limitTime || limitTime <= 0) {
                return
            }
            if (!nowShowActivityName.isNullOrEmpty()) {
                val nameArr = nowShowActivityName!!.split(".")
                val name = if (!nameArr.isNullOrEmpty()) {
                    if (nameArr.size > 1) {
                        nameArr[nameArr.size - 1]
                    } else {
                        nameArr[0]
                    }
                } else {
                    null
                }
                if (name != null) {
                    var isLimit = false
                    for (n in limitName) {
                        if (name == n) {
                            isLimit = true
                            break
                        }
                    }
                    if (!isLimit) {
                        val intent = when (mode) {
                            1 -> {
                                val i = Intent(application, com.idopartx.phonelightning.ui.SplashActivity::class.java)
                                i.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                                i.putExtra("isIcon", isOnCreate)
                                i
                            }
                            2 -> {
                                if (CLApplication.isFullVideoIng) {
                                    return
                                }
                                val i = Intent(application, FullVideoActivity::class.java)
                                i.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                                i
                            }
                            else -> {
                                null
                            }
                        }
                        if (intent != null) {
                            application.startActivity(intent)
                        }
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

}